//
//  UILabel+Extension.swift
//  Library_y
//
//  Created by JC on 2023/6/6.
//

import Foundation
import UIKit

public extension UILabel {
    
    /// 设置label的颜色大小
    ///
    /// - Parameters:
    ///   - color: 颜色
    ///   - font: 字体大小
    func set(_ color: UIColor = .black,
                    _ font: UIFont = Font.dp(16)) {
        textColor = color
        self.font = font
    }
    
    /// 设置label的颜色大小
    ///
    /// - Parameters:
    ///   - color: 颜色
    ///   - font: 字体大小
    ///   - alignment: 字体位置
    func set(_ color: UIColor = .black,
                    _ font: UIFont = Font.dp(16),
                    _ alignment: NSTextAlignment = NSTextAlignment.left) {
        textColor = color
        self.font = font
        textAlignment = alignment
    }
}

public extension UILabel {
    
    /// 设置文本并两端对齐
    ///
    /// - parameter text:  文本
    /// - parameter color: 文本颜色
    func setJustified(text: String, color: UIColor?) {
        self.text = text
        let justifiedColor = color ?? textColor
        justified(color: justifiedColor ?? .black)
    }
    
    /// 调整当前文本为两端对齐
    ///
    /// - parameter color: 文本颜色
    func justified(color: UIColor = .black) {
        guard let text = self.text else { return }
        let size = CGSize(width: frame.width, height: CGFloat.greatestFiniteMagnitude)
        let textSize = (text as NSString).boundingRect(with: size, options: [NSStringDrawingOptions.usesLineFragmentOrigin, NSStringDrawingOptions.truncatesLastVisibleLine, NSStringDrawingOptions.usesFontLeading], attributes: [NSAttributedString.Key.font: font as Any], context: nil)
        let margin = (frame.size.width - textSize.width) / CGFloat(text.count - 1)
        let number = NSNumber(value: Float(margin))
        let attrText = NSMutableAttributedString(string: text)
        attrText.addAttributes([NSAttributedString.Key(rawValue: kCTKernAttributeName as String as String): number, NSAttributedString.Key.foregroundColor: color], range: NSMakeRange(0, text.count - 1))
        attrText.addAttributes([NSAttributedString.Key.foregroundColor: color], range: NSMakeRange(0, text.count))
        attributedText = attrText
    }
    
    /// 设置行间距
    ///
    /// - Parameter space: 行间距
    func setRow(space: CGFloat = 3) {
        guard let text = text else { return }
        let attributedString = NSMutableAttributedString(string: text)
        let paragraphStye = NSMutableParagraphStyle()
        //调整行间距
        paragraphStye.lineSpacing = space
        let rang = NSMakeRange(0, CFStringGetLength(text as CFString?))
        attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStye, range: rang)
        self.attributedText = attributedString
    }
    
    /// 设置不同颜色
    func setColor(_ color: UIColor, range: NSRange) {
        guard let text = text else { return }
        let attributedString = NSMutableAttributedString(string: text)
        attributedString.addAttribute(.foregroundColor, value: color, range: range)
        self.attributedText = attributedString
    }
    
    /// 设置不同字体大小
    func setFont(specialFont: UIFont, range: NSRange) {
        guard let text = text else { return }
        let attributedString = NSMutableAttributedString(string: text)
        attributedString.addAttributes([NSAttributedString.Key.font: specialFont], range: range)
        self.attributedText = attributedString
    }
    
    
    /// 设置行间距与字体颜色
    ///
    /// - Parameters:
    ///   - color: 颜色
    ///   - range: 范围
    ///   - space: 行间距
    func setColorAndRow(_ color: UIColor, range: NSRange, space: CGFloat = 3) {
        guard let text = text else { return }
        let attributedString = NSMutableAttributedString(string: text)
        let paragraphStye = NSMutableParagraphStyle()
        //调整行间距
        paragraphStye.lineSpacing = space
        let rang = NSMakeRange(0, CFStringGetLength(text as CFString?))
        attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStye, range: rang)
        attributedString.addAttribute(.foregroundColor, value: color, range: range)
        self.attributedText = attributedString
    }
    
    /// 中间添加图片
    ///
    /// - Parameters:
    ///   - imgRect: 图片的大小
    ///   - location: 图片的位置
    func addImage(imgRect: CGRect, img: UIImage , location: Int) {
        guard let text = text else { return }
        let attributedString = NSMutableAttributedString(string: text)
        let attach = NSTextAttachment()
        attach.image = img
        attach.bounds = imgRect
        let string = NSAttributedString(attachment: attach)
        attributedString.insert(string, at: location)
        self.attributedText = attributedString
    }
}

public extension UILabel {
    
    /// html文本
    func loadHtml(_ html: String) {
        guard let htmlData = html.data(using: String.Encoding.unicode) else { return }
        attributedText = try? NSAttributedString(data: htmlData, options:  [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)
    }
    
    /// 文本行数
    ///
    /// - parameter lineWidth: 行宽
    ///
    /// - returns: 行数
    func lineCount(lineWidth: CGFloat) -> Int {
        return lround(Double(intrinsicContentSize.width / lineWidth)) + 1
    }
    
    /// 添加颜色属性文本
    ///
    /// - parameter text:  文本
    /// - parameter color: 文本的颜色属性
    func addColorAttributedText(text: String, color: UIColor) {
        let attr = [NSAttributedString.Key.foregroundColor: color]
        addAttributedText(text: text, attrs: attr)
    }
    
    /// 添加字体属性文本
    ///
    /// - parameter text: 文本
    /// - parameter font: 文本的字体属性
    func addFontAttributedText(text: String, font: UIFont) {
        let attr = [NSAttributedString.Key.font: font]
        addAttributedText(text: text, attrs: attr)
    }
    
    /// 添加颜色字体属性文本
    ///
    /// - parameter text:  文本
    /// - parameter color: 文本的颜色属性
    /// - parameter font:  文本的字体属性
    func addColorFontAttributedText(text: String, color: UIColor, font: UIFont) {
        let attrs = [NSAttributedString.Key.foregroundColor: color, NSAttributedString.Key.font: font]
        addAttributedText(text: text, attrs: attrs)
    }
    
    /// 添加属性文本
    ///
    /// - parameter text:  文本
    /// - parameter attrs: 文本的属性
    func addAttributedText(text: String, attrs: [NSAttributedString.Key: Any]) {
        
        var attributedText: NSMutableAttributedString
        
        if let originAttributedText = self.attributedText {
            attributedText = NSMutableAttributedString(attributedString: originAttributedText)
            attributedText.mutableString.append(text)
        } else {
            attributedText = NSMutableAttributedString(string: text)
        }
        
        let originTextLength = self.attributedText?.string.count ?? 0
        let newTextLength = text.count
        
        attributedText.addAttributes(attrs, range: NSRange(location: originTextLength, length: newTextLength))
        self.attributedText = attributedText
    }
}
